﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WorkFlowBLL;

namespace QLCF.Forms
{
    public partial class popupPayment : Form
    {
        public popupPayment()
        {
            InitializeComponent();
        }
        private int billID = -1;        
        private frmTableService frmTableS = null;
        public popupPayment(frmTableService frmTservice, int m_billID, ref bool m_show,bool m_permissionPayment)
        {
            InitializeComponent();
            if (!m_permissionPayment)
            {
                btnPayment.Enabled = false;
                btnPaymentIn.Enabled = false;
            }
            billID = m_billID;
            frmTableS = frmTservice;
            CtrProduct ctrP = new CtrProduct();
            var infoC=ctrP.BillGetInfoCommon(billID);
            if ((infoC == null) || (infoC.TotalMoney <= 0))
            {
                MessageBox.Show("Tổng tiền trong đơn hàng = 0!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Close();
                m_show = false;
                return;
            }
            lblTableName.Text = infoC.TableName;
            lblTotalMoney.Text = infoC.TotalMoney.ToString("0,0") + " VNĐ";
            m_show = true;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btnPaymentIn_Click(object sender, EventArgs e)
        {
            CtrProduct ctrP = new CtrProduct();
            if (ctrP.BillClose(billID) <= 0)
            {
                MessageBox.Show("Lỗi đóng hóa đơn!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CtrAdmin ctra = new CtrAdmin();
                var infoc = ctrP.BillGetInfoCommon(billID);
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, "Thanh toán và in hóa đơn bàn \"" + infoc.TableName + " mã: " + infoc.TableCode + " (" + infoc.RangeName + ")" + "\"" + " mã hóa đơn: " + infoc.BillID, "Quản lý dịch vụ");
                Reports.frmViewBillDetailsRpt frm = new Reports.frmViewBillDetailsRpt(billID, true);
                frm.ShowDialog();
                frmTableS.RefreshCurrentTab();
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Reports.frmViewBillDetailsRpt frm = new Reports.frmViewBillDetailsRpt(billID, false);
            frm.ShowDialog();
        }

        private void btnPayment_Click(object sender, EventArgs e)
        {
            CtrProduct ctrP = new CtrProduct();
            if (ctrP.BillClose(billID) <= 0)
            {
                MessageBox.Show("Lỗi đóng hóa đơn!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                CtrAdmin ctra = new CtrAdmin();
                var infoc = ctrP.BillGetInfoCommon(billID);
                ctra.LogInsert(Program.UserEntity.UserID, Program.UserEntity.UserName, 2, "Thanh toán hóa đơn bàn \"" + infoc.TableName + " mã: " + infoc.TableCode + " (" + infoc.RangeName + ")" + "\"" + " mã hóa đơn: " + infoc.BillID, "Quản lý dịch vụ");
                frmTableS.RefreshCurrentTab();
            }
            this.Close();
        }
    }
}
